//
//  BIZCCLiveSwitchView.m
//  biz
//
//  Created by zhangfuwei on 2017/3/29.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIZCCLiveSwitchView.h"

@implementation BIZCCLiveSwitchView{
    BIZCCLiveSwitchViewSwitchBlock  _switchBlock;
    UIImageView     *_backGroudImgV;
    UIButton        *_leftBtn;
    UIButton        *_rightBtn;
    UIImageView     *_leftBottomLine;
    UIImageView     *_rightBOttomLine;
    BOOL            _isChating;
}

- (instancetype)initWithSwitchBlock:(BIZCCLiveSwitchViewSwitchBlock)switchBlock
{
    self = [super init];
    if (self) {
        _switchBlock    = switchBlock;
        _isChating      = YES;
        self.backgroundColor = [UIColor clearColor];
        [self loadUI];
    }
    return self;
}

- (void)loadUI {
    
    _backGroudImgV = [[UIImageView alloc] init];
    _backGroudImgV.backgroundColor = [UIColor blackColor];
    _backGroudImgV.alpha = 0.4;
    
    _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _leftBottomLine = [UIImageView new];
    _rightBOttomLine = [UIImageView new];
    
    [_leftBtn setTitle:@"直播互动" forState:UIControlStateNormal];
    [_rightBtn setTitle:@"问题发布" forState:UIControlStateNormal];
    
    
    _leftBtn.titleLabel.font = CHINESE_SYSTEM(15);
    _rightBtn.titleLabel.font = CHINESE_SYSTEM(15);
    
    _leftBtn.layer.cornerRadius = 15;
    _rightBtn.layer.cornerRadius = 15;
    
    _rightBtn.clipsToBounds = YES;
    _leftBtn.clipsToBounds = YES;
    _leftBtn.tag = 1;
    _rightBtn.tag = 2;
    
    [_leftBtn addTarget:self action:@selector(switchChatOrAnswerWithButton:) forControlEvents:UIControlEventTouchUpInside];
    [_rightBtn addTarget:self action:@selector(switchChatOrAnswerWithButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [self switchButtonBackGroudViewWithTag:1];
    
    [self addSubview:_backGroudImgV];
    [self addSubview:_leftBtn];
    [self addSubview:_rightBtn];
    [self addSubview:_leftBottomLine];
    [self addSubview:_rightBOttomLine];

}

-(void)layoutSubviews {
    [super layoutSubviews];

    [_backGroudImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [_leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.centerX.equalTo(self).offset(-1*68);
        make.width.equalTo(@136);
        make.height.equalTo(@42);
    }];
    [_rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.centerX.equalTo(self).offset(68);
        make.width.height.equalTo(_leftBtn);
    }];
    [_leftBottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_leftBtn.mas_bottom);
        make.bottom.equalTo(self);
        make.centerX.equalTo(_leftBtn);
        make.width.equalTo(@20);
    }];
    
    [_rightBOttomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.bottom.equalTo(_leftBottomLine);
        make.centerX.equalTo(_rightBtn);
    }];
}

- (void)switchChatOrAnswerWithButton:(UIButton *)btn {
    switch (btn.tag) {
        case 1:{
            
            _isChating = YES;
            [self switchButtonBackGroudViewWithTag:btn.tag];
        }
            break;
        case 2:{
            
            _isChating = NO;
            [self switchButtonBackGroudViewWithTag:btn.tag];
            
        }
            break;
   
        default:
            break;
    }
    
}

- (void)switchButtonBackGroudViewWithTag:(NSInteger)tag {
    
    _switchBlock ? _switchBlock(tag) : nil;
//    [_leftBtn setBackgroundImage:_isChating ? [UIImage imageNamed:@"ccLiveRoom_sliderBg_2"] :nil forState:UIControlStateNormal];
//    [_rightBtn setBackgroundImage:!_isChating ? [UIImage imageNamed:@"ccLiveRoom_sliderBg_2"] : nil forState:UIControlStateNormal];
    
    [_leftBtn setTitleColor:_isChating ? HEXCOLOR(0xefb90f):HEXCOLOR(0xffffff) forState:UIControlStateNormal];
     
    [_rightBtn setTitleColor:_isChating ? HEXCOLOR(0xffffff):HEXCOLOR(0xefb90f) forState:UIControlStateNormal];
    [_leftBottomLine setImage:_isChating ? [UIImage imageNamed:@"ccLiveRoom_switchLine"]:[UIImage imageNamed:@"ccLiveRoom_switchLine2"]];
    [_rightBOttomLine setImage:_isChating ? [UIImage imageNamed:@"ccLiveRoom_switchLine2"]:[UIImage imageNamed:@"ccLiveRoom_switchLine"]];

    

}

@end
